home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
- Date: 10 Feb 1996 23:07:47 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb10160747@qcd.lanl.gov>
- References: <4ep87b$o60@alcor.usc.edu> <harmon.823155385@pegasus.montclair.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: harmon@pegasus.montclair.edu's message of 1 Feb 1996 01:18:41 -0500
-
- In article <harmon.823155385@pegasus.montclair.edu>
- harmon@pegasus.montclair.edu (Derek Harmon) writes:
- <snip>
- > int m[2][2] = {1, 2, 3, 4};
-
- I always m[2][2] = { {1, 2}, {3, 4} }; else it gets very confusing.
- >
- > myfunc(m,2,2);
-
- Since the array name m devolves into a pointer to an int which is
- the first element of the array, *array = 1 in the function when called
- in this way.
-
- This is incorrect. After `int m[2][2]', m decays to a pointer to the
- first _subarray of int_ and has the type `int (*)[2]'. It does not become an
- int * ... and with myfunc's prototype in scope, a compiler must give
- an error because there is no auto conversion from int (*)[2] to int*.
-
- > So why can't C convert from pointer to integer to a multidemsional
- > array. The reason I want to do this is because my function needs to
- > take in an array of any width and height. Thank you for your time,
-
- A dynamic array huh? You need to think in pointers (note the
- plurality) Arrays are really pointers, the more dimensions, the more
- pointers you need.
-
- The FAQ has a much better discussion of this.
-
-
- Try,
-
- : void yourfunc(int *array[], int rows, int cols)
-
- The (int *)array can be indexed with one [] index in your function,
- and then you can use the other [] to index rows (or is it columns?) :)
- What it really comes down to is int **array, a double pointer! Think
- about it. C is full of hobgoblins, most are more gruesome though.
-
- Remember, a pointer to a pointer is not a pointer to an array. Hence,
- with 'int m[2][2];', yourfunc(m,2,2) is equally wrong: in fact more
- wrong if that makes sense.
-
- Cheers
- Tanmoy--
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-